package org.jeecg.modules.api.controller.music.home;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.music.entity.MusArtist;
import org.jeecg.modules.music.entity.MusSong;
import org.jeecg.modules.music.entity.MusTopList;
import org.jeecg.modules.music.entity.vo.HomeAlbumsVo;
import org.jeecg.modules.music.entity.vo.HomeHotAlbumsVo;
import org.jeecg.modules.music.entity.vo.HomeTopListVo;
import org.jeecg.modules.music.service.IMusAlbumSongListService;
import org.jeecg.modules.music.service.IMusArtistService;
import org.jeecg.modules.music.service.IMusTopListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Api(tags="音乐系统")
@RestController
@RequestMapping("api/mus/")
public class HomeHotAlbumsAPIController {
    @Autowired
    private IMusAlbumSongListService musAlbumSongListService;

    @Autowired
    private IMusArtistService musArtistService;

    @Autowired
    private IMusTopListService musTopListService;

    @ApiOperation("API-首页歌单专辑热门推荐-热门歌单")
    @RequestMapping(value = "/personalized", method = RequestMethod.GET)
    public Result<List<HomeAlbumsVo>> queryHomeHotAlbums(@RequestParam(name = "type", defaultValue = "1") String type, @RequestParam(name = "limit", defaultValue = "8") String limit, @RequestParam(name = "area",required = false) String area){
        Result<List<HomeAlbumsVo>> result = new Result<>();
        try {
            if(oConvertUtils.isNotEmpty(type)){
                List<HomeAlbumsVo> list = musAlbumSongListService.queryHomeHotAlbums(type,limit,area);
                List<HomeAlbumsVo> records = list.stream().map(HomeAlbumsVo -> {
                    if(HomeAlbumsVo.getType()==1){
                        HomeAlbumsVo.setCopywriter("热门推荐");
                    }
                    return HomeAlbumsVo;
                }).collect(Collectors.toList());
                result.setResult(records);
            }else{
                result.setResult(null);
            }
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }

    @ApiOperation("API-首页歌单专辑热门推荐-热门专辑")
    @RequestMapping(value = "/album/newest", method = RequestMethod.GET)
    public Result<List<HomeHotAlbumsVo>> queryHomeAlbumsAll(){
        Result<List<HomeHotAlbumsVo>> result = new Result<>();
        try {
            List<HomeHotAlbumsVo> albums  = musAlbumSongListService.queryHotAlbumsAll();
            List<HomeHotAlbumsVo> list = albums.stream().map(HomeHotAlbumsVo -> {
                if (oConvertUtils.isNotEmpty(HomeHotAlbumsVo.getArtistId())){
                    HomeHotAlbumsVo.setArtist(musArtistService.getById(HomeHotAlbumsVo.getArtistId()));
                }
               return HomeHotAlbumsVo;
            }).collect(Collectors.toList());
            result.setResult(list);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }

    @ApiOperation("API-首页歌单专辑热门推荐-入驻歌手")
    @RequestMapping(value = "/top/artists", method = RequestMethod.GET)
    public Result<IPage<MusArtist>> queryHomeArtist( @RequestParam(name="offset", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="limit", defaultValue="5") Integer pageSize, HttpServletRequest req) {
        Result<IPage<MusArtist>> result = new Result<IPage<MusArtist>>();
        Page<MusArtist> page = new Page<MusArtist>(pageNo, pageSize);
        QueryWrapper<MusArtist> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_enter",1).eq("del_flag",0);


        IPage<MusArtist> pageList = musArtistService.page(page,queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }

    @ApiOperation("首页榜单")
    @RequestMapping(value = "/toplist", method = RequestMethod.GET)
    public Result<IPage<MusTopList>> queryHomeArtist(@RequestParam(name="offset", defaultValue="1") Integer pageNo,
                                                     @RequestParam(name="limit", defaultValue="50") Integer pageSize) {
        Result<IPage<MusTopList>> result = new Result<IPage<MusTopList>>();
        Page<MusTopList> page = new Page<MusTopList>(pageNo, pageSize);
        QueryWrapper<MusTopList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("toplist_type",1).orderByAsc("sort");


        IPage<MusTopList> pageList = musTopListService.page(page,queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @ApiOperation("API-首页歌单专辑热门推荐-榜单详情")
    @RequestMapping(value = "/playlist/detail", method = RequestMethod.GET)
    public Result<HomeTopListVo> detailTopList(@RequestParam(name="id",required = true) String id){

        MusTopList topList = musTopListService.getById(id);
        List<MusSong> songs = musTopListService.querySongsByListId(id);
        HomeTopListVo homeTopListVo = new HomeTopListVo();

        homeTopListVo.setId(topList.getId());
        homeTopListVo.setName(topList.getName());
        homeTopListVo.setCoverImgUrl(topList.getAvatar());
        homeTopListVo.setPlaylist(songs);

        Result<HomeTopListVo> result = new Result<>();
        result.setSuccess(true);
        result.setResult(homeTopListVo);
        return result;
    }


}
